<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./OL_SDK/include-openlayers-local.js"></script>
    <script src="./libs/gaode.js"></script>
    <script src="https://lib.baomitu.com/jquery/2.2.4/jquery.js"></script>
    <script src="https://lib.baomitu.com/Turf.js/latest/turf.min.js"></script>
</head>

<body>
    <div id="map">
    </div>
    <script>
        var map = new ol.Map({
            target: "map",
            layers: [gaode],
            view: new ol.View({
                projection: 'EPSG:4326',
                center: [114.30, 30.50],
                zoom: 4
            })
        })
        var url = 'https://restapi.amap.com/v3/ip?key=db774d5285ed8eb97c1d816f8e671c94'
        /* 设置城市中心点 */
        /* geojson */

        /* 图层 --中心点 */
        const style = new ol.style.Style({
            /**{olx.style.IconOptions}类型*/
            image: new ol.style.Icon({
                anchor: [0.5, 60],
                anchorOrigin: 'top-right',
                anchorXUnits: 'fraction',
                anchorYUnits: 'pixels',
                offsetOrigin: 'top-right',
                // offset:[0,10],
                //图标缩放比例
                scale: 0.5,
                //透明度
                opacity: 0.75,
                //图标的url
                src: '/images/location.png',
            }),
            stroke: new ol.style.Stroke({
                color: 'green',
                width: 10,
            })
        })
        const source = new ol.source.Vector({

        })
        const layer = new ol.layer.Vector({
            source,
            style
        })
        map.addLayer(layer)
        $.ajax({
            url
        }).then(data => {
            console.log(data)
            let { adcode, rectangle } = data;
            var arr = rectangle.split(";")

            var result = [];
            arr.forEach(item => {
                result.push(item.split(","))
            })// 二维数组（线）
            console.log(result, 'result')
            const line = new ol.Feature({
                geometry: new ol.geom.LineString(result)
            })
            // console.log(result)
            var point1 = turf.point(result[0]);// 转为geojson中的一个feature对象
            var point2 = turf.point(result[1]);
            // console.log(point1, 'point1')
            var midpoint = turf.midpoint(point1, point2);
            // console.log(midpoint,'midpoint')// 生成的还是geojson中的一个feature对象
            /* 设置中心点 */
            // Works with both a single feature and a feature collection.
            var features = new ol.format.GeoJSON().readFeatures(midpoint);
            // 加载非标准geojson  // 生成的也是一个数组，不过里边只有一个feature对象
            // console.log(features, 'features')
            source.addFeatures(features)
            console.log(line, 'line')
            source.addFeature(line)
            /* // Only works for a single feature
            let feature = new ol.format.GeoJSON().readFeature(midpoint)
            source.addFeature(feature) */

            /* geojson */
            var geojson_url = `https://geo.datav.aliyun.com/areas_v3/bound/${adcode}.json`;
            var geo_source = new ol.source.Vector({
                format: new ol.format.GeoJSON(),
                url: geojson_url
            })
            var geo_layer = new ol.layer.Vector({
                source: geo_source
            })
            map.addLayer(geo_layer)
        })
    </script>
</body>

</html>